home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / INPUT / EVENT.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  19.3 KB  |  512 lines

  1.  
  2. package sub_arctic.input;
  3.  
  4. import sub_arctic.lib.interactor;
  5. import sub_arctic.lib.top_level;
  6.  
  7. import java.awt.Point;
  8. import java.awt.Event;
  9.  
  10. /** 
  11.  * This class represents an input event.  It is a wrapper around 
  12.  * java.awt.Event and encapsulates its public instance variables (providing
  13.  * access methods instead).  In general for each public instance variable "v" 
  14.  * in the original Event class there are access methods "v()" and "set_v()" 
  15.  * in this class.  One exception is that the x and y fields have been renamed
  16.  * global_x and global_y to better reflect their meaning in subArctic. In 
  17.  * addition, this class introduces fields to maintain position in an object's 
  18.  * local coordinate system and to record the root top_level object along with 
  19.  * methods to manipulate these.  
  20.  *
  21.  * @see java.awt.Event
  22.  * @author Scott Hudson
  23.  */
  24. public class event {
  25.  
  26.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  27.  
  28.   /** The AWT event object being wrapped */
  29.   protected Event _awt_event;
  30.  
  31.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  32.  
  33.   /** AWT field: target component.  */
  34.   public Object target() {return _awt_event.target;}
  35.  
  36.   /** Set AWT field: target component.  */
  37.   public void set_target(Object o) {_awt_event.target = o;}
  38.  
  39.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  40.  
  41.   /** AWT field: time stamp.  */
  42.   public long when() {return _awt_event.when;}
  43.  
  44.   /** Set AWT field: time stamp.  */
  45.   public void set_when(long w) {_awt_event.when = w;}
  46.  
  47.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  48.  
  49.   /** AWT field: type of this event. */
  50.   public int id() {return _awt_event.id;}
  51.  
  52.   /** Set AWT field: type of this event. */
  53.   public void set_id(int i) {_awt_event.id = i;}
  54.  
  55.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  56.  
  57.   /** AWT field: x coordinate of the event.  In our case this is the 
  58.    *  global position (position in the top_level root object).  */
  59.   public int global_x() {return _awt_event.x;}
  60.  
  61.   /** Set AWT field: x coordinate of the event.  In our case this is the 
  62.    *  global position (position in the top_level root object).  */
  63.   public void set_global_x(int xv) {_awt_event.x = xv;}
  64.  
  65.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  66.  
  67.   /** AWT field: y coordinate of the event.  In our case this is the 
  68.    *  global position (position in the top_level root object).  */
  69.   public int global_y() {return _awt_event.y;}
  70.  
  71.   /** Set AWT field: y coordinate of the event.  In our case this is the 
  72.    *  global position (position in the top_level root object).  */
  73.   public void set_global_y(int yv) {_awt_event.y = yv;}
  74.  
  75.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  76.  
  77.   /** AWT field: key that was pressed in a keyboard event. */
  78.   public int key() {return _awt_event.key;}
  79.  
  80.   /** Set AWT field: key that was pressed in a keyboard event. */
  81.   public void set_key(int k) {_awt_event.key = k;}
  82.  
  83.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  84.  
  85.   /** AWT field: state of the modifier keys.  */
  86.   public int modifiers() {return _awt_event.modifiers;}
  87.  
  88.   /** Set AWT field: state of the modifier keys.  */
  89.   public void set_modifiers(int m) {_awt_event.modifiers = m;}
  90.  
  91.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  92.  
  93.   /** AWT field: The number of consecutive clicks. This field is relevant 
  94.    *  only for MOUSE_DOWN events. If the field isn't set it will be 0. 
  95.    *  Otherwise, it will be 1 for single-clicks, 2 for double-clicks, and so on.
  96.    */
  97.   public int clickCount() {return _awt_event.clickCount;}
  98.  
  99.   /** Set AWT field: The number of consecutive clicks. This field is relevant 
  100.    *  only for MOUSE_DOWN events. If the field isn't set it will be 0. 
  101.    *  Otherwise, it will be 1 for single-clicks, 2 for double-clicks, and so on.
  102.    */
  103.   public void set_clickCount(int c) {_awt_event.clickCount = c;}
  104.  
  105.   /** Alias for clickCount() using subArctic style name. */
  106.   public int click_count() {return clickCount();}
  107.  
  108.   /** Alias for set_clickCount() using subArctic style name. */
  109.   public void set_click_count(int c) {set_clickCount(c);}
  110.  
  111.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  112.  
  113.   /** AWT field: An arbitrary argument. */
  114.   public Object arg() {return _awt_event.arg;}
  115.  
  116.   /** set AWT field: An arbitrary argument. */
  117.   public void set_arg(Object a) {_awt_event.arg = a;}
  118.  
  119.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  120.  
  121.   /** AWT field: The next event. Used when putting events into a linked list. */
  122.   public Event evt() {return _awt_event.evt;}
  123.  
  124.   /** AWT field: The next event. Used when putting events into a linked list. */
  125.   public void evt(Event e) {_awt_event.evt = e;}
  126.  
  127.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  128.  
  129.   /** X position in local coordinates of the object it is delivered to. 
  130.    *  The global_x field will retain the global coordinate value.  */
  131.   protected int _local_x;
  132.  
  133.   /** X position in local coordinates of the object it is delivered to. 
  134.    *  The global_x field will retain the global coordinate value.  */
  135.   public int local_x() {return _local_x;}
  136.  
  137.   /** Set the X position in local coordinates of the object it is delivered to. 
  138.    *  The global_x field will retain the global coordinate value.  */
  139.   public void set_local_x(int x) {_local_x = x;}
  140.  
  141.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  142.  
  143.   /** Y position in local coordinates of the object it is delivered to. 
  144.    *  The global_y field will retain the global coordinate value.  */
  145.   protected int _local_y;
  146.  
  147.   /** Y position in local coordinates of the object it is delivered to. 
  148.    *  The global_y field will retain the global coordinate value.  */
  149.   public int local_y() {return _local_y;}
  150.  
  151.   /** Set the Y position in local coordinates of the object it is delivered to. 
  152.    *  The global_y field will retain the global coordinate value.  */
  153.   public void set_local_y(int y) {_local_y = y;}
  154.  
  155.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  156.  
  157.   /** Top level interactor that this event occured in.  Global coordinates 
  158.    *  are with respect to the origin of that object.  */
  159.   protected top_level _root_interactor;
  160.  
  161.   /** Top level interactor that this event occured in.  Global coordinates 
  162.    *  are with respect to the origin of that object.  */
  163.   public top_level root_interactor() {return _root_interactor;}
  164.  
  165.   /** Set the top level interactor that this event occured in.  Global 
  166.    *  coordinates are with respect to the origin of that object.  Note, this
  167.    *  does not update coordinates for you and in general needs to be used
  168.    *  with great care.
  169.    */
  170.   public void set_root_interactor(top_level t) {_root_interactor = t;}
  171.  
  172.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  173.  
  174.   /** 
  175.    * Construct one of these events from a normal AWT event. 
  176.    * @param Event     other the AWT event we are creating this from.
  177.    * @param top_level root  the subArctic root object that this event is 
  178.    *                        associated with (this provides its global 
  179.    *                        coordinate system).
  180.    */
  181.   public event(Event other, top_level root)
  182.     {
  183.       /* Initialize a new awt event from the other */
  184.       _awt_event = new Event(other.target, other.when, other.id, 
  185.                   other.x, other.y, other.key, other.modifiers, other.arg);
  186.       
  187.       /* plus the part they don't let you send to the constructor */
  188.       _awt_event.clickCount = other.clickCount;
  189.  
  190.       /* plus our additions -- start out with local == global */
  191.       _local_x = other.x;
  192.       _local_y = other.y;
  193.  
  194.       _root_interactor = root;
  195.     }
  196.  
  197.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  198.  
  199.   /** 
  200.    * Copy constructor. 
  201.    * @param event other the event we are making a copy of.
  202.    */
  203.   public event(event other)
  204.     {
  205.       this(other._awt_event, other.root_interactor());
  206.       _local_x  = other._local_x;
  207.       _local_y  = other._local_y;
  208.     }
  209.  
  210.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  211.  
  212.   /** 
  213.    * Change event which is currently placed in coords of the parent of the 
  214.    * given object into one placed in the local coords of the given object.
  215.    * This only works if we know the coordinate system of the event in advance.
  216.    * If this is now know, use global_to_local() instead.<p>
  217.    *
  218.    * @see sub_arctic.input.event#global_to_local
  219.    * @interactor of_obj the interactor object in question.
  220.    */
  221.   public void into_local(interactor of_obj)
  222.     {
  223.       Point local_pt = of_obj.into_local(new Point(local_x(),local_y()));
  224.       set_local_x(local_pt.x);
  225.       set_local_y(local_pt.y);
  226.     }
  227.  
  228.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  229.  
  230.   /** 
  231.    * Change event which is currently placed in the given object's local 
  232.    * coords into one placed in the coords of its parent.
  233.    * This only works if we know the coordinate system of the event in advance.
  234.    * If this is not know, use reset_to_global() to get the event into a 
  235.    * known coordinate system.
  236.    * <p>
  237.    *
  238.    * @see sub_arctic.input.event#into_local
  239.    * @see sub_arctic.input.event#global_to_local
  240.    * @see sub_arctic.input.event#reset_to_global
  241.    * @interactor of_obj the interactor object in question.
  242.    */
  243.   public void into_parents(interactor of_obj)
  244.     {
  245.       Point local_pt = of_obj.into_parent(new Point(local_x(),local_y()));
  246.       set_local_x(local_pt.x);
  247.       set_local_y(local_pt.y);
  248.     }
  249.  
  250.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  251.  
  252.   /** Put event into the given object's local coordinates regardless of what
  253.    *  coordinates it is in now. */
  254.   public void global_to_local(interactor of_obj)
  255.     {
  256.       Point local_pt = of_obj.global_to_local(new Point(global_x(),global_y()));
  257.       set_local_x(local_pt.x);
  258.       set_local_y(local_pt.y);
  259.     }
  260.  
  261.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  262.  
  263.   /** Force event back into global coordinates */
  264.   public void reset_to_global()
  265.     {
  266.       set_local_x(global_x());
  267.       set_local_y(global_y());
  268.     }
  269.  
  270.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  271.  
  272.   /** 
  273.    * Create a human readable string corresponding to an event id code. 
  274.    * @param int id the event id code.
  275.    * @return String a string with the human readable name of the event type.
  276.    */
  277.   public static String id_string(int id)
  278.     {
  279.       switch (id) {
  280.     case Event.ACTION_EVENT:    return "ACTION_EVENT"; 
  281.     case Event.GOT_FOCUS:        return "GOT_FOCUS"; 
  282.     case Event.KEY_ACTION:        return "KEY_ACTION"; 
  283.     case Event.KEY_ACTION_RELEASE:    return "KEY_ACTION_RELEASE"; 
  284.     case Event.KEY_PRESS:        return "KEY_PRESS"; 
  285.     case Event.KEY_RELEASE:        return "KEY_RELEASE"; 
  286.     case Event.LIST_DESELECT:    return "LIST_DESLECT"; 
  287.     case Event.LIST_SELECT:        return "LIST_SELECT"; 
  288.     case Event.LOAD_FILE:        return "LOAD_FILE"; 
  289.     case Event.LOST_FOCUS:        return "LOST_FOCUS"; 
  290.     case Event.MOUSE_DOWN:        return "MOUSE_DOWN"; 
  291.     case Event.MOUSE_DRAG:        return "MOUSE_DRAG"; 
  292.     case Event.MOUSE_ENTER:        return "MOUSE_ENTER"; 
  293.     case Event.MOUSE_EXIT:        return "MOUSE_EXIT"; 
  294.     case Event.MOUSE_MOVE:        return "MOUSE_MOVE"; 
  295.     case Event.MOUSE_UP:        return "MOUSE_UP"; 
  296.     case Event.SAVE_FILE:        return "SAVE_FILE"; 
  297.     case Event.SCROLL_ABSOLUTE:    return "SCROLL_ABSOLUTE"; 
  298.     case Event.SCROLL_LINE_DOWN:    return "SCROLL_LINE_DOWN"; 
  299.     case Event.SCROLL_LINE_UP:    return "SCROLL_LINE_UP"; 
  300.     case Event.SCROLL_PAGE_DOWN:    return "SCROLL_PAGE_DOWN"; 
  301.     case Event.SCROLL_PAGE_UP:    return "SCROLL_PAGE_UP"; 
  302.     case Event.WINDOW_DEICONIFY:    return "WINDOW_DEICONIFY"; 
  303.     case Event.WINDOW_DESTROY:    return "WINDOW_DESTROY"; 
  304.     case Event.WINDOW_EXPOSE:    return "WINDOW_EXPOSE"; 
  305.     case Event.WINDOW_ICONIFY:    return "WINDOW_ICONIFY"; 
  306.     case Event.WINDOW_MOVED:    return "WINDOW_MOVED"; 
  307.     default:                        return "UNKNOWN_ID:" + id;
  308.       }
  309.     }
  310.  
  311.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  312.  
  313.   /** 
  314.    * Create a human readable string corresponding to an event key code. 
  315.    * @param int key the key code.
  316.    * @return String a string with the human readable name of the key code.
  317.    */
  318.   public static String key_string(int key)
  319.     {
  320.       switch (key) {
  321.     case Event.DOWN:    return "DOWN";
  322.     case Event.END:        return "END";
  323.     case Event.F1:        return "F1";
  324.     case Event.F2:        return "F2";
  325.     case Event.F3:        return "F3";
  326.     case Event.F4:        return "F4";
  327.     case Event.F5:        return "F5";
  328.     case Event.F6:        return "F6";
  329.     case Event.F7:        return "F7";
  330.     case Event.F8:        return "F8";
  331.     case Event.F9:        return "F9";
  332.     case Event.F10:        return "F10";
  333.     case Event.F11:        return "F11";
  334.     case Event.F12:        return "F12";
  335.     case Event.HOME:    return "HOME";
  336.     case Event.LEFT:    return "LEFT";
  337.     case Event.PGDN:    return "PGDN";
  338.     case Event.PGUP:    return "PGUP";
  339.     case Event.RIGHT:    return "RIGHT";
  340.     case Event.UP:        return "UP";
  341.     default:        
  342.       if (key > 31 && key < 127) 
  343.         return "\"" + (char)key + "\"";
  344.       else 
  345.         return "" + key;
  346.       }
  347.     }
  348.  
  349.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  350.  
  351.   /** 
  352.    * Create a human readable string corresponding to an event modifier mask. 
  353.    * @param int mod the modifier mask.
  354.    * @return String a string with the human readable dump of the modifier mask.
  355.    */
  356.   public static String mod_string(int mod)
  357.     {
  358.       StringBuffer result = new StringBuffer();
  359.       if ((mod & Event.SHIFT_MASK) != 0) result.append("SHIFT ");
  360.       if ((mod & Event.CTRL_MASK) != 0) result.append("CTRL ");
  361.       if ((mod & Event.META_MASK) != 0) result.append("META ");
  362.       if ((mod & Event.ALT_MASK) != 0) result.append("ALT");
  363.       return result.toString();
  364.     }
  365.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  366.  
  367.   /** Convert event to a human readable string which describes it. */
  368.   public String toString()
  369.     {
  370.       StringBuffer result = new StringBuffer();
  371.  
  372.       result.append("sub_arctic.event[global_x,y=");
  373.       result.append(global_x()); result.append(","); result.append(global_y()); 
  374.       result.append(",localx,y=");
  375.       result.append(local_x()); result.append(","); result.append(local_y()); 
  376.       result.append(",id="); result.append(id_string(id()));
  377.       result.append(",key="); result.append(key_string(key()));
  378.       result.append(",mod="); result.append(mod_string(modifiers()));
  379.       result.append("]");
  380.  
  381.       return result.toString();
  382.     }
  383.  
  384.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  385.  
  386.   /* constants copied from Event */
  387.   public static final int SHIFT_MASK         = Event.SHIFT_MASK;
  388.   public static final int CTRL_MASK         = Event.CTRL_MASK;
  389.   public static final int META_MASK         = Event.META_MASK;
  390.   public static final int ALT_MASK         = Event.ALT_MASK;
  391.   public static final int HOME             = Event.HOME;
  392.   public static final int END             = Event.END;
  393.   public static final int PGUP             = Event.PGUP;
  394.   public static final int PGDN             = Event.PGDN;
  395.   public static final int UP             = Event.UP;
  396.   public static final int DOWN             = Event.DOWN;
  397.   public static final int LEFT             = Event.LEFT;
  398.   public static final int RIGHT         = Event.RIGHT;
  399.   public static final int F1            = Event.F1;
  400.   public static final int F2            = Event.F2;
  401.   public static final int F3            = Event.F3;
  402.   public static final int F4            = Event.F4;
  403.   public static final int F5            = Event.F5;
  404.   public static final int F6            = Event.F6;
  405.   public static final int F7            = Event.F7;
  406.   public static final int F8            = Event.F8;
  407.   public static final int F9            = Event.F9;
  408.   public static final int F10            = Event.F10;
  409.   public static final int F11            = Event.F11;
  410.   public static final int F12            = Event.F12;
  411.   public static final int WINDOW_DESTROY     = Event.WINDOW_DESTROY;
  412.   public static final int WINDOW_EXPOSE     = Event.WINDOW_EXPOSE;
  413.   public static final int WINDOW_ICONIFY    = Event.WINDOW_ICONIFY;
  414.   public static final int WINDOW_DEICONIFY    = Event.WINDOW_DEICONIFY;
  415.   public static final int WINDOW_MOVED        = Event.WINDOW_MOVED;
  416.   public static final int KEY_PRESS         = Event.KEY_PRESS;
  417.   public static final int KEY_RELEASE         = Event.KEY_RELEASE;
  418.   public static final int KEY_ACTION         = Event.KEY_ACTION;
  419.   public static final int KEY_ACTION_RELEASE    = Event.KEY_ACTION_RELEASE;
  420.   public static final int MOUSE_DOWN         = Event.MOUSE_DOWN;
  421.   public static final int MOUSE_UP         = Event.MOUSE_UP;
  422.   public static final int MOUSE_MOVE         = Event.MOUSE_MOVE;
  423.   public static final int MOUSE_ENTER         = Event.MOUSE_ENTER;
  424.   public static final int MOUSE_EXIT         = Event.MOUSE_EXIT;
  425.   public static final int MOUSE_DRAG         = Event.MOUSE_DRAG;
  426.   public static final int SCROLL_LINE_UP    = Event.SCROLL_LINE_UP;
  427.   public static final int SCROLL_LINE_DOWN    = Event.SCROLL_LINE_DOWN;
  428.   public static final int SCROLL_PAGE_UP    = Event.SCROLL_PAGE_UP;
  429.   public static final int SCROLL_PAGE_DOWN    = Event.SCROLL_PAGE_DOWN;
  430.   public static final int SCROLL_ABSOLUTE    = Event.SCROLL_ABSOLUTE;
  431.   public static final int LIST_SELECT        = Event.LIST_SELECT;
  432.   public static final int LIST_DESELECT        = Event.LIST_DESELECT;
  433.   public static final int ACTION_EVENT        = Event.ACTION_EVENT;
  434.   public static final int LOAD_FILE        = Event.LOAD_FILE;
  435.   public static final int SAVE_FILE        = Event.SAVE_FILE;
  436.   public static final int GOT_FOCUS        = Event.GOT_FOCUS;
  437.   public static final int LOST_FOCUS        = Event.LOST_FOCUS;
  438.  
  439.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  440.  
  441.   /** 
  442.    * AWT method:
  443.    * Translates an event relative to the given component. This
  444.    * involves at a minimum translating the coordinates so they make
  445.    * sense within the given component. It may also involve
  446.    * translating a region in the case of an expose event.<br>
  447.    *
  448.    * For subArctic this affects the global position of the event.  (You rarely 
  449.    * want or need to do this). The local  position is adjusted accordingly. 
  450.    *
  451.    * @param x the x coordinate
  452.    * @param y the y coordinate
  453.    */
  454.   public void translate(int x, int y) 
  455.     {
  456.       _awt_event.translate(x,y);
  457.       set_local_x(x + local_x());
  458.       set_local_y(y + local_y());
  459.     }
  460.  
  461.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  462.  
  463.   /**
  464.    * AWT method:
  465.    * Checks if the shift key is down.
  466.    * @see #modifiers
  467.    * @see #controlDown
  468.    * @see #metaDown
  469.    */
  470.   public boolean shiftDown() {return _awt_event.shiftDown();}
  471.  
  472.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  473.  
  474.   /**
  475.    * AWT method:
  476.    * Checks if the control key is down.
  477.    * @see #modifiers
  478.    * @see #shiftDown
  479.    * @see #metaDown
  480.    */
  481.   public boolean controlDown() {return _awt_event.controlDown();}
  482.  
  483.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  484.  
  485.   /**
  486.    * AWT method:
  487.    * Checks if the meta key is down.
  488.    * @see #modifiers
  489.    * @see #shiftDown
  490.    * @see #controlDown
  491.    */
  492.   public boolean metaDown() {return _awt_event.metaDown();}
  493.  
  494.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  495. }
  496. /*=========================== COPYRIGHT NOTICE ===========================
  497.  
  498. This file is part of the subArctic user interface toolkit.
  499.  
  500. Copyright (c) 1996 Scott Hudson and Ian Smith
  501. All rights reserved.
  502.  
  503. The subArctic system is freely available for most uses under the terms
  504. and conditions described in 
  505.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  506. and appearing in full in the lib/interactor.java source file.
  507.  
  508. The current release and additional information about this software can be 
  509. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  510.  
  511. ========================================================================*/
  512.